package com.xinqi.modules.sys.role.manager.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.modules.sys.role.domain.RoleEntity;
import com.xinqi.modules.sys.role.dto.req.RoleQueryDTO;
import com.xinqi.modules.sys.role.dto.req.RoleStatusUpdateDTO;
import com.xinqi.modules.sys.role.dto.rsp.RoleResultDTO;
import com.xinqi.modules.sys.role.manager.RoleManager;
import com.xinqi.modules.sys.role.mapper.RoleMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 系统角色管理层实现
 *
 * @author: linkai
 * @date: 2023/04/01
 */
@Service
@RequiredArgsConstructor
public class RoleManagerImpl extends BaseServiceImpl<RoleMapper, RoleEntity> implements RoleManager {
    /**
     * 自定义分页查询
     */
    @Override
    public Page<RoleResultDTO> findPage(Page<Object> page, RoleQueryDTO query) {
        return this.baseMapper.findPage(page, query);
    }

    /**
     * 自定义列表查询
     */
    @Override
    public List<RoleResultDTO> findList(RoleQueryDTO query) {
        return this.baseMapper.findList(query);
    }

    /**
     * 根据id查询 系统角色数据
     */
    @Override
    public RoleEntity findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return getById(id);
    }

    /**
     * 根据id删除 系统角色数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return this.removeById(id);
    }

    @Override
    public boolean updateStatus(RoleStatusUpdateDTO dto) {
        LambdaUpdateWrapper<RoleEntity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(RoleEntity::getId, dto.getRoleId());
        wrapper.set(RoleEntity::getStatus, dto.getStatus());
        return update(null, wrapper);
    }

    @Override
    public List<RoleResultDTO> findUserRole(Long userId) {
        return this.baseMapper.findUserRole(userId);
    }

}
